package com.matrix.maprender;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.tiled.TileAtlas;
import com.badlogic.gdx.graphics.g2d.tiled.TileMapRenderer;
import com.badlogic.gdx.graphics.g2d.tiled.TiledLayer;
import com.badlogic.gdx.graphics.g2d.tiled.TiledLoader;
import com.badlogic.gdx.graphics.g2d.tiled.TiledMap;
import com.badlogic.gdx.math.Vector3;
import com.matrix.dungeon.Generate;

public class Render implements ApplicationListener {	
	TileMapRenderer tileMapRender;
	OrthographicCamera camera;
	Texture t;
	SpriteBatch sb;
	Vector3 spritePosition = new Vector3();
	float rotationSpeed = 0.5f;
//	Vector3 tmp = new Vector3();
	//SpriteBatch spriteBatch = new SpriteBatch();
	
	@Override
	public void create() {
		t = new Texture(Gdx.files.internal("blue_space.jpg"));
		sb = new SpriteBatch();
//		Generate gen = new Generate();
//		int[][][] tileIds = gen.generate();
//		int[][][] tmp = new int[1][4][4];
//		tmp[0][0][0] = 1;
//		TMXGenerator tmxGen = new TMXGenerator(tileIds);
//		tmxGen.writeTMX("test.tmx");
//		TiledMap map = tmxGen.getTileMap();
		FileHandle mapHandle = Gdx.files.internal("data/test.tmx");
		TiledMap map = TiledLoader.createMap(mapHandle);
		TileAtlas atlas = new TileAtlas(map, Gdx.files.internal("data/"));
		tileMapRender = new TileMapRenderer(map, atlas, 1, 1);

		camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		camera.position.set(Gdx.graphics.getWidth() / 2,  Gdx.graphics.getHeight() / 2, 0);
		camera.zoom = 3f;
	}

	@Override
	public void dispose() {
	}

	@Override
	public void pause() {
		
	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		 
    	camera.update();
		handleInput();
//		tmp.set(0, 0, 0);
//		camera.unproject(tmp);
		tileMapRender.render(camera);
		
		sb.begin();
		sb.draw(t, spritePosition.x, spritePosition.y);
		sb.end();
		
		if(Gdx.input.isTouched()) {
            camera.unproject(spritePosition.set(Gdx.input.getX(), Gdx.input.getY(), 0));
		}
	}
	private void handleInput() {
        if(Gdx.input.isKeyPressed(Input.Keys.A)) {
                camera.zoom += 0.2f;
        }
        if(Gdx.input.isKeyPressed(Input.Keys.Q)) {
                camera.zoom -= 0.2f;
        }
        if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
                if (camera.position.x > 0)
                        camera.translate(-3, 0, 0);
        }
        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
                if (camera.position.x < 1024)
                        camera.translate(3, 0, 0);
        }
        if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
                if (camera.position.y > 0)
                        camera.translate(0, -3, 0);
        }
        if(Gdx.input.isKeyPressed(Input.Keys.UP)) {
                if (camera.position.y < 1024)
                        camera.translate(0, 3, 0);
        }
        if(Gdx.input.isKeyPressed(Input.Keys.W)) {
                camera.rotate(-rotationSpeed, 0, 0, 1);
        }
        if(Gdx.input.isKeyPressed(Input.Keys.E)) {
                camera.rotate(rotationSpeed, 0, 0, 1);
        }
}

	@Override
	public void resize(int arg0, int arg1) {
		
	}

	@Override
	public void resume() {
		
	}
}
